<%@page import="edu.gatech.team404.Account"%>
<%@page import="edu.gatech.team404.dal.AccountDAL"%>
<%@page import="edu.gatech.team404.tool.StatusCode"%>
<%@page import="java.util.ArrayList"%>

<%@include file="header.jsp" %>

<div id="content">
    <%@include file="sidebar.jsp" %>
    
    <%@include file="toolbar.jsp" %>
    
    <div id="main">
    	<h1 align="center">Account</h1>
    	
    	<div id="create">
	    	<div class="status">
		    	<%
		    		Object status = session.getAttribute("accountCreationStatus");
		    		if (status != null) {
		    			StatusCode code = (StatusCode) status;
		    			switch (code) {
			    			case ACCOUNT_CREATE_SUCCESS :
			    				out.println("<p style=\"color:blue;\">");
								out.println("Account has been successfully created.");
								out.println("</p>");
			    				break;
			    			case ACCOUNT_CREATE_FAIL :
			    				out.println("<p style=\"color:red;\">");
								out.println("Cannot create an Account. Please try again later.");
								out.println("</p>");
			    				break;
			    			case ACCOUNT_CREATE_MISMATCHED_PASSWORD :
			    				out.println("<p style=\"color:red;\">");
								out.println("Passwords do not match.");
								out.println("</p>");
			    				break;
			    			case ACCOUNT_CREATE_INVALID_DATA :
			    				out.println("<p style=\"color:red;\">");
								out.println("Invalid input(s).");
								out.println("</p>");
			    				break;
		    			}
		    		}
		    	%>
	    	</div>
	    	
	    	<div class="viewTable">
		    	<form action="Register" method="post">
		    		<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    		<table cellpadding="10">
		    			<tr>
		    				<th align="right">User name</th>
		    				<td align="left"><input type="text" name="username" value="" size="50" maxlength="15"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Password</th>
		    				<td align="left"><input type="password" name="password1" value="" size="50" maxlength="15"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Confirm Password</th>
		    				<td align="left"><input type="password" name="password2" value="" size="50" maxlength="15"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Name</th>
		    				<td align="left"><input type="text" name="name" value="" size="50" maxlength="50"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Email</th>
		    				<td align="left"><input type="text" name="email" value="" size="50" maxlength="30"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Type</th>
		    				<td align="left">
			    				<select name="type">
									<option value="Admin">Administrator</option>
									<option value="Instructor">Instructor</option>
									<option value="Student">Student</option>
								</select>
							</td>
		    			</tr>
		    			<tr>
		    				<td colspan="2" align="center">
		    					<input type="submit" value="Create Account!" />
		    				</td>
		    			</tr>
		    		</table>
		    	</form>
	    	</div>
    	</div>
    	
    	<div id="review">
	    	<table class="viewTable" border="1" cellpadding="10">
	    		<tr>
	    			<th>Username</th>
	    			<th>Password</th>
	    			<th>Name</th>
	    			<th>Email</th>
	    			<th>Type</th>
	    			<th>Status</th>
	    		</tr>
	
		    	<% 
		    		ArrayList<Account> accts = (ArrayList<Account>) acctDal.getAllAccounts();
		    		acctDal.close();
		    		
		    		for (Account acct : accts) {
		    			if (acct != null) {
		    				out.println ("<tr>");
		    				out.println ("<td>" + acct.getUsername() + "</td>");
		    				out.println ("<td>" + acct.getPassword() + "</td>");
		    				out.println ("<td>" + acct.getName() + "</td>");
		    				out.println ("<td>" + acct.getEmail() + "</td>");
		    				out.println ("<td>" + acct.getType() + "</td>");
		    				out.println ("<td>" + acct.getStatus() + "</td>");
		    				out.println ("</tr>");
		    			}
		    		}
	    		%>
	    	
	    	</table>
    	</div>
    	
    	<div id="update">
    		<div class="status">
		    	<%
		    		status = session.getAttribute("accountUpdateStatus");
		    		if (status != null) {
		    			StatusCode code = (StatusCode) status;
		    			switch (code) {
			    			case ACCOUNT_UPDATE_SUCCESS :
			    				out.println("<p style=\"color:blue;\">");
								out.println("Account has been successfully updated.");
								out.println("</p>");
			    				break;
			    			case ACCOUNT_UPDATE_FAIL :
			    				out.println("<p style=\"color:red;\">");
								out.println("Cannot update the Account. Please try again later.");
								out.println("</p>");
			    				break;
			    			case ACCOUNT_UPDATE_MISMATCHED_PASSWORD :
			    				out.println("<p style=\"color:red;\">");
								out.println("Passwords do not match.");
								out.println("</p>");
			    				break;
			    			case ACCOUNT_UPDATE_INVALID_DATA :
			    				out.println("<p style=\"color:red;\">");
								out.println("Invalid input(s).");
								out.println("</p>");
			    				break;
		    			}
		    		}
		    	%>
	    	</div>
	    	
	    	<div>
		    	<form name="updateAccountForm" action="UpdateAccount" method="post">
		    		<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    		<table class="viewTable" cellpadding="10">
		    			<tr>
		    				<th align="right">User name</th>
		    				<td>
			    				<select name="username" onChange="Script404.accountUpdate(this.value, 'updateAccountForm')"> 
			    					<option value="">Select username</option> 
				    				<%
				    					for(Account acct : accts){
				    						if (acct != null) {
				    		    				out.println ("<option value=\"" + acct.getUsername() + "\">" + acct.getUsername() + "</option>");
				    						}
				    					}
				    				%>
		    					</select>
		    				</td>
		    			</tr>
		    			<tr>
		    				<th align="right">Password</th>
		    				<td align="left"><input type="password" name="password1" value="" size="50" maxlength="15"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Confirm Password</th>
		    				<td align="left"><input type="password" name="password2" value="" size="50" maxlength="15"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Name</th>
		    				<td align="left"><input type="text" name="name" value="" size="50" maxlength="50"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Email</th>
		    				<td align="left"><input type="text" name="email" value="" size="50" maxlength="30"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Status</th>
		    				<td>
			    				<select name="status">
			    					<option value="Unknown">Unknown</option>
			    					<option value="Active">Active</option>
			    					<option value="Suspended">Suspended</option>
			    				</select>
		    				</td>
		    			</tr>
		    			<tr>
		    				<td colspan="2" align="center">
		    					<input type="submit" value="Update Account!" />
		    				</td>
		    			</tr>
		    		</table>
		    	</form>
	    	</div>
    	</div>
    	
    	<div id="delete">
    		<div class="status">
		    	<%
		    		status = session.getAttribute("accountDeleteStatus");
		    		if (status != null) {
		    			StatusCode code = (StatusCode) status;
		    			switch (code) {
			    			case ACCOUNT_DELETE_SUCCESS :
			    				out.println("<p style=\"color:blue;\">");
								out.println("Account has been successfully deleted.");
								out.println("</p>");
			    				break;
			    			case ACCOUNT_DELETE_FAIL :
			    				out.println("<p style=\"color:red;\">");
								out.println("Cannot delete the Account. Please try again later.");
								out.println("</p>");
			    				break;
			    			case ACCOUNT_DELETE_INVALID_DATA :
			    				out.println("<p style=\"color:red;\">");
								out.println("Invalid input(s).");
								out.println("</p>");
			    				break;
		    			}
		    		}
		    	%>
	    	</div>
	    	
	    	<div>
		    	<form action="DeleteAccount" method="post">
		    		<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    		<table class="viewTable" cellpadding="10">
		    			<tr>
		    				<th align="right">User name</th>
		    				<td>
			    				<select name="username" onChange="Script404.accountDesc(this.value, 'accountDescription')">
			    					<option value="">Select username</option>
				    				<%
				    					for(Account acct : accts){
				    						if (acct != null) {
				    		    				out.println ("<option value=\"" + acct.getUsername() + "\">" + acct.getUsername() + "</option>");
				    						}
				    					}
				    				%>
		    					</select>
		    				</td>
		    			</tr>
		    			<tr>
							<td colspan=2 align""=left">
								<p id="accountDescription"></p>
							</td>
						</tr>
		    			<tr>
		    				<td colspan="2" align="center">
		    					<input type="submit" value="Delete Account!" />
		    				</td>
		    			</tr>
		    		</table>
		    	</form>
	    	</div>
    	</div>
    	
    	<script type="text/javascript"> 
			Script404.initShow("<%= session.getAttribute("feed") %>"); 	   
		</script> 
    </div>
</div>

<%@include file="footer.jsp" %>